import time

from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import TimeoutException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver import Remote

from common.find_img import FindImg
from common.report_add_img import add_img_2_report
from common.tools import get_project_path, build_path


class Base:
    def find_element_new(self,driver,locator_type=AppiumBy.XPATH,locator_value=None,timeout=10):
        """
        :param driver: 驱动
        :param locator_type: 定位的类型
        :param locator_value: 定位的值
        :param timeout: 显示等待的时间
        :return: 返回元素
        """

        try:
            element=WebDriverWait(driver,timeout).until(
                EC.presence_of_element_located((locator_type,locator_value))
            )
            return element
        except(TimeoutException,NoSuchElementException):
            print(f'元素未找到{locator_type},{locator_value}')
            return None

    def find_elements_new(self,driver,locator_type=AppiumBy.XPATH,locator_value=None,timeout=10):
        """
        :param driver: 驱动
        :param locator_type: 定位的类型
        :param locator_value: 定位的值
        :param timeout: 显示等待的时间
        :return: 返回元素
        """

        try:
            element=WebDriverWait(driver,timeout).until(
                EC.presence_of_all_elements_located((locator_type,locator_value))
            )
            return element
        except(TimeoutException,NoSuchElementException):
            print(f'元素未找到{locator_type},{locator_value}')
            return None

    def send_keys_new(self, driver, locator_type, locator_value, value ,timeout=10):
        try:
            element=self.find_element_new(driver,locator_type,locator_value,timeout=10)
            element.clear()
            element.send_keys(value)
        except Exception as E:
            print(f'点击元素{locator_type,locator_value}失败:{str(E)}')

    def click_new(self, driver, locator_type, locator_value,timeout=10):
        try:
            element = self.find_element_new(driver, locator_type, locator_value,timeout=10)
            element.click()
        except Exception as E:
            print(f'点击元素{locator_type,locator_value}失败:{str(E)}')

    def get_text(self,driver,locator_type=AppiumBy.XPATH,locator_value=None):
        element=self.find_element_new(driver,locator_type,locator_value)
        try:
            element_text=element.text
            return element_text
        except Exception as e:
            print(f'元素text获取失败，错误信息为{e}')

    def find_img_in_source(self, driver, img_name):
        """
        截图并在截图中查找图片
        :param driver:
        :param img_name:
        :return:
        """
        # 截图后图片保存的路径
        source_img_path = get_project_path() + build_path("img", "source_img", img_name, add_sep_before=True)
        # 需要查找的图片的路径
        search_img_path = get_project_path() + build_path("img", "assert_img", img_name, add_sep_before=True)
        # 截图并保存图片
        driver.get_screenshot_as_file(source_img_path)
        time.sleep(3)
        # 在原图中查找是否有指定的图片来返回我们的信心值
        confidence = FindImg().get_confidence(source_img_path, search_img_path)
        return confidence

class BaseAssert:
    def assert_equal_screen_shot(self,driver,exceptor,step_name):
        if not isinstance(exceptor,tuple):
            print("exceptor类型传入错误，必须重新传入元组类型")
        else:
            try:
                print(f"正在断言{exceptor[0]}是否等于{exceptor[1]}")
                assert exceptor[0]==exceptor[1]
                add_img_2_report(driver,step_name)
            except Exception as e:
                print(f"断言失败：{str(e)}")
                add_img_2_report(driver, step_name)
















